package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.CommodityStockRecordService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.CommodityStockRecordDto;
import com.huilian.iotbox.data.dto.OrderDto;
import com.huilian.iotbox.data.po.CommodityStockRecord;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;

@RequestMapping("/api/commodityStockRecord")
@RestController
public class CommodityStockRecordController extends BaseController {

    @Autowired
    private UserService userService;
    @Resource
    private CommodityStockRecordService commodityStockRecordService;


    @PostMapping("/findCommodityStockRecordByMultipleConditions")
    public ServerResponse findCommodityStockRecordByMultipleConditions(@RequestBody CommodityStockRecordDto commodityStockRecordDto, HttpServletRequest request){
        UserInfo userInfo = userService.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        commodityStockRecordDto.setPageNum(Integer.parseInt(request.getParameter("pageNum")));
        commodityStockRecordDto.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
        return ServerResponse.createBySuccess(commodityStockRecordService.findCommodityStockRecordByMultipleConditions(commodityStockRecordDto));
    }
    @PostMapping("/export/list")
    public ServerResponse exportList(HttpServletRequest request, @RequestBody CommodityStockRecordDto commodityStockRecordDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        if(commodityStockRecordDto.getCreateTime()==null){
            return ServerResponse.createByErrorMessage("请求异常，导出时间间隔建议在一个月内");
        }
        boolean flag = compareReqTimeSectionMonth(commodityStockRecordDto.getCreateTime(),commodityStockRecordDto.getUpdateTime());
        if(!flag){
            return ServerResponse.createByErrorMessage("请求异常，导出时间间隔建议在一个月内");
        }
        return commodityStockRecordService.exportList(commodityStockRecordDto);
    }
    private boolean compareReqTimeSectionMonth(Date beginTime, Date endTime) {
        Calendar calendar = Calendar.getInstance(); // 得到日历
        if (beginTime == null) {
            return false;
        }
        calendar.setTime(beginTime); // 把当前时间赋给日历
        calendar.add(Calendar.MONTH, 1); // 添加一个月
        beginTime = calendar.getTime();
        int i = beginTime.compareTo(endTime);
        return i >= 0;
    }

    /**
     * 库存统计
     * */
    @PostMapping("/stockStatisticList")
    public ServerResponse stockStatisticList(@RequestBody CommodityStockRecordDto commodityStockRecordDto, HttpServletRequest request){
        UserInfo userInfo = userService.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        commodityStockRecordDto.setPageNum(Integer.parseInt(request.getParameter("pageNum")));
        commodityStockRecordDto.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
        return ServerResponse.createBySuccess(commodityStockRecordService.stockStatisticList(commodityStockRecordDto));
    }

    @PostMapping("/exportStockStatistic/list")
    public ServerResponse exportStockStatisticList(HttpServletRequest request, @RequestBody CommodityStockRecordDto commodityStockRecordDto) {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        if(commodityStockRecordDto.getCreateTime()==null){
            return ServerResponse.createByErrorMessage("请求异常，导出时间间隔建议在一个月内");
        }
        boolean flag = compareReqTimeSectionMonth(commodityStockRecordDto.getCreateTime(),commodityStockRecordDto.getUpdateTime());
        if(!flag){
            return ServerResponse.createByErrorMessage("请求异常，导出时间间隔建议在一个月内");
        }
        return commodityStockRecordService.exportStockStatisticList(commodityStockRecordDto);
    }

    /**
     * 统计总数 不分组
     * */

    @PostMapping("/stockStatistic")
    public ServerResponse stockStatistic(@RequestBody CommodityStockRecordDto commodityStockRecordDto, HttpServletRequest request){
        UserInfo userInfo = userService.getLoginUserInfo(request);
        commodityStockRecordDto.setUserInfo(userInfo);
        return ServerResponse.createBySuccess(commodityStockRecordService.stockStatistic(commodityStockRecordDto));
    }
}
